#include "spi.h"

#define TIMEOUT_VAL     (0xFFFFFFFFUL)
uint32_t SPI_ReadWrite(uint32_t data)
{
    uint32_t timeout = 0;

    // FL_SPI_SetSSNPin(SPI1, FL_SPI_SSN_LOW);

    FL_SPI_WriteTXBuff(SPI1, data);

    do
    {
        timeout++;
    } while(!FL_SPI_IsActiveFlag_TXBuffEmpty(SPI1) && timeout < TIMEOUT_VAL);

    timeout = 0;
    do
    {
        timeout++;
    } while(!FL_SPI_IsActiveFlag_RXBuffFull(SPI1) && timeout < TIMEOUT_VAL);

    data = FL_SPI_ReadRXBuff(SPI1);

    // FL_SPI_SetSSNPin(SPI1, FL_SPI_SSN_HIGH);

    return data;
}
